﻿//**********
//Author: PL, createdate: 2012.08.10
//**********

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Resources;
using System.Globalization;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

namespace LPV_POS.mh_pos.mh_danhmuc
{
    public partial class mh_dmTheThanhToan : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        CultureInfo culture;
        string str_lang = "";
        #region "Muti language"

        private void SetLanguage_dmTheThanhToan(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);

                sbtn_Cancel.Text = rm.GetString("thoat", culture);
                sbtn_Delete.Text = rm.GetString("xoa", culture);
                sbtn_Edit.Text = rm.GetString("sua", culture);
                sbtn_Save.Text = rm.GetString("luu", culture);
                sbtn_Add.Text = rm.GetString("them", culture);

                this.col_OBJID.Caption = rm.GetString("ma", culture);
                this.col_OBJNAME.Caption = rm.GetString("thethanhtoan", culture);
                lbl_CardName.Text = rm.GetString("ten", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion
        public mh_dmTheThanhToan()
        {
            InitializeComponent();
        }

        private void sbtn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mh_dmTheThanhToan_Load(object sender, EventArgs e)
        {

            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_dmTheThanhToan(str_lang);

            grdThethanhtoan_Load();
        }

        #region "xử lý database"
        private DataTable f_GetAll()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose"};
            o = new object[] { 1,1};
            tb = tv.SP_SelectDataTable("sp_mh_dmTheTT", s, o);

            return tb;
        }

        // insert mot the TT
        private int f_insertTheTT(string strTenthe)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME"};
            o = new object[] { 1, 2, txt_CardName.Text };
            int ketqua = tv.SP_Update("sp_mh_dmTheTT", s, o);
            return ketqua;
        }
        //update the TT
        private int f_updateTheTT(string maTheTT, string tenTheTT)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME"};
            o = new object[] { 1, 3, maTheTT, tenTheTT };
            int ketqua = tv.SP_Update("sp_mh_dmTheTT", s, o);
            return ketqua;
        }

        // xoa The TT
        private int f_deleteTheTT(string maTheTT)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 1, 4, maTheTT };
            int ketqua = tv.SP_Update("sp_mh_dmTheTT", s, o);
            return ketqua;
        }

        #endregion

        //load hình thức thẻ thanh toán
        public void grdThethanhtoan_Load()
        {
            grv_Thethanhtoan.OptionsBehavior.Editable = false;
            grd_Thethanhtoan.MainView = grv_Thethanhtoan;
            grd_Thethanhtoan.DataSource = f_GetAll();
        }

        private void sbtn_nhap_Click(object sender, EventArgs e)
        {
            txt_CardName.Text = new cpublic().loadformchu_point(250, 200);
        }

        private void sbtn_Add_Click(object sender, EventArgs e)
        {
            txt_CardName.Text = "";
            sbtn_nhap.Enabled = true;
            sbtn_Save.Enabled = true;
            Variable.str_trangthaithemsua = "them";
        }

        private void sbtn_Edit_Click(object sender, EventArgs e)
        {
            sbtn_nhap.Enabled = true;
            sbtn_Save.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void sbtn_Save_Click(object sender, EventArgs e)
        {
            int chiso = -1;

            if (txt_CardName.Text == "")    // không thể luu neu chua nhap ten TheTT
                return;
            string tenTheTT = txt_CardName.Text;
           
            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertTheTT(tenTheTT);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)grd_Thethanhtoan.Views[0]).FocusedRowHandle;
                string maTheTT = grv_Thethanhtoan.GetRowCellValue(grv_Thethanhtoan.FocusedRowHandle, col_OBJID).ToString();
                ketqua = f_updateTheTT(maTheTT, tenTheTT);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }

            }
            grd_Thethanhtoan.DataSource = null;
            grd_Thethanhtoan.DataSource = f_GetAll();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grv_Thethanhtoan.SelectRow(chiso);
                grv_Thethanhtoan.FocusedRowHandle = chiso;
            }
        }

        private void sbtn_Delete_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (grd_Thethanhtoan.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string maTheTT = grv_Thethanhtoan.GetRowCellValue(grv_Thethanhtoan.FocusedRowHandle, col_OBJID).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deleteTheTT(maTheTT);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi the thanh toan
                int chiso;
                chiso = ((ColumnView)grd_Thethanhtoan.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)grd_Thethanhtoan.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void grv_Thethanhtoan_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grv_Thethanhtoan.FocusedColumn;
            txt_CardName.Text = grv_Thethanhtoan.GetRowCellDisplayText(chiso, col_OBJNAME) + "";
            
            sbtn_nhap.Enabled = false;
            sbtn_Save.Enabled = false;
        }
    }
}